var t = d(require("../../../@babel/runtime/regenerator")),
  e = require("../../../@babel/runtime/helpers/asyncToGenerator"),
  a = d(require("../../../config/index")),
  n = require("@/utils/index"),
  i = require("@/utils/jump"),
  r = d(require("@/config/page")),
  o = require("@/services/base/api"),
  l = (function (t) {
    if (t && t.__esModule) return t;
    if (null === t || ("object" != typeof t && "function" != typeof t))
      return { default: t };
    var e = u();
    if (e && e.has(t)) return e.get(t);
    var a = {},
      n = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var i in t)
      if (Object.prototype.hasOwnProperty.call(t, i)) {
        var r = n ? Object.getOwnPropertyDescriptor(t, i) : null;
        r && (r.get || r.set) ? Object.defineProperty(a, i, r) : (a[i] = t[i]);
      }
    (a.default = t), e && e.set(t, a);
    return a;
  })(require("@/services/search-service")),
  s = d(require("@libs/lodash")),
  c = require("@/utils/report");
function u() {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap();
  return (
    (u = function () {
      return t;
    }),
    t
  );
}
function d(t) {
  return t && t.__esModule ? t : { default: t };
}
Page({
  data: {
    title: "搜索",
    position: !1,
    background: "#FAF3E8",
    iconTheme: "#000",
    titleColor: "#000",
    CDN: a.default.CDN,
    defaultImg: a.default.defaultImg,
    tabs: ["全部", "位置", "展览", "藏品"],
    val: "",
    isSearched: !1,
    historyList: [],
    activity0: [],
    exhibition0: null,
    collection0: null,
    collection: null,
    exhibition: null,
    activity: null,
    current: 0,
    res: null,
    show: !1,
    isOver1: !1,
    isOver2: !1,
    isOver3: !1,
    isLoad1: !1,
    isLoad2: !1,
    isLoad3: !1,
    pn1: 1,
    pn2: 1,
    pn3: 1,
    lat: 1,
    lon: 1,
    total1: 0,
    total2: 0,
    total3: 0,
  },
  onLoad: function () {
    var a = this;
    return e(
      t.default.mark(function e() {
        var n;
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), (0, o.getHistoryList)();
              case 2:
                (n = t.sent), a.setData({ historyList: n });
              case 4:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  onShow: function () {
    var a = this;
    wx.getLocation({
      type: "wgs84",
      success: function (n) {
        return e(
          t.default.mark(function e() {
            return t.default.wrap(function (t) {
              for (;;)
                switch ((t.prev = t.next)) {
                  case 0:
                    a.setData({ lat: n.latitude, lon: n.longitude });
                  case 1:
                  case "end":
                    return t.stop();
                }
            }, e);
          }),
        )();
      },
    });
  },
  getSearch: function (t) {
    var e = t.detail.value.trim();
    this.setData({ val: e }), e && this.search(e);
  },
  search: s.default.debounce(
    e(
      t.default.mark(function e() {
        var a, i, r, o, s, c, u, d, f, v, h, p, x, b, y, g, w, D, m, k;
        return t.default.wrap(
          function (t) {
            for (;;)
              switch ((t.prev = t.next)) {
                case 0:
                  return (
                    (m = this.data.val),
                    (t.next = 3),
                    Promise.all([
                      l.positionSearch({
                        pn: 1,
                        keyword: m,
                        lat: this.data.lat,
                        lon: this.data.lon,
                        rn: 5,
                      }),
                      l.exhibitSearch({ pn: 1, keyword: m, rn: 5 }),
                      l.collectionSearch({ pn: 1, keyword: m, rn: 5 }),
                    ])
                  );
                case 3:
                  (k = t.sent),
                    this.setData({
                      activity:
                        null !== (a = k[0]) &&
                        void 0 !== a &&
                        null !== (i = a.data) &&
                        void 0 !== i &&
                        i.list
                          ? k[0].data.list.map(function (t) {
                              return (
                                (t.distance = (0, n.getShowDistance)(
                                  t.distance,
                                )),
                                t
                              );
                            })
                          : [],
                      exhibition:
                        null !== (r = k[1]) &&
                        void 0 !== r &&
                        null !== (o = r.data) &&
                        void 0 !== o &&
                        o.list
                          ? k[1].data.list.map(function (t) {
                              return (
                                (t.distance = (0, n.getShowDistance)(
                                  t.distance,
                                )),
                                t
                              );
                            })
                          : [],
                      collection:
                        null !== (s = k[2]) &&
                        void 0 !== s &&
                        null !== (c = s.data) &&
                        void 0 !== c &&
                        c.list
                          ? k[2].data.list.map(function (t) {
                              return (
                                (t.distance = (0, n.getShowDistance)(
                                  t.distance,
                                )),
                                t
                              );
                            })
                          : [],
                      activity0:
                        null !== (u = k[0]) &&
                        void 0 !== u &&
                        null !== (d = u.data) &&
                        void 0 !== d &&
                        d.list
                          ? k[0].data.list.slice(0, 3)
                          : [],
                      exhibition0:
                        null !== (f = k[1]) &&
                        void 0 !== f &&
                        null !== (v = f.data) &&
                        void 0 !== v &&
                        v.list
                          ? k[1].data.list.slice(0, 3)
                          : [],
                      collection0:
                        null !== (h = k[2]) &&
                        void 0 !== h &&
                        null !== (p = h.data) &&
                        void 0 !== p &&
                        p.list
                          ? k[2].data.list.slice(0, 3)
                          : [],
                      total1:
                        null !== (x = k[0]) &&
                        void 0 !== x &&
                        null !== (b = x.data) &&
                        void 0 !== b &&
                        b.total
                          ? k[0].data.total
                          : 0,
                      total2:
                        null !== (y = k[1]) &&
                        void 0 !== y &&
                        null !== (g = y.data) &&
                        void 0 !== g &&
                        g.total
                          ? k[1].data.total
                          : 0,
                      total3:
                        null !== (w = k[2]) &&
                        void 0 !== w &&
                        null !== (D = w.data) &&
                        void 0 !== D &&
                        D.total
                          ? k[2].data.total
                          : 0,
                      show: !0,
                      isLoad1: !1,
                      isLoad2: !1,
                      isLoad3: !1,
                    }),
                    this.isLoadMore();
                case 6:
                case "end":
                  return t.stop();
              }
          },
          e,
          this,
        );
      }),
    ),
    300,
  ),
  isLoadMore: function () {
    var t = this.data,
      e = t.activity,
      a = t.exhibition,
      n = t.collection,
      i = t.activity0,
      r = t.exhibition0,
      o = t.collection0,
      l = t.total1,
      s = t.total2,
      c = t.total3;
    this.setData({
      isOver1: e.length >= l,
      isOver2: a.length >= s,
      isOver3: n.length >= c,
      res: i.concat(r).concat(o),
    });
  },
  onConfirm: function (a) {
    var n = this;
    return e(
      t.default.mark(function e() {
        var i;
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                if (a.detail.value.trim()) {
                  t.next = 3;
                  break;
                }
                return t.abrupt("return");
              case 3:
                if (
                  (n.setData({ isSearched: !0, current: 0 }),
                  (i = { name: n.data.val.trim() }),
                  (t.t0 = i.name),
                  !t.t0)
                ) {
                  t.next = 9;
                  break;
                }
                return (t.next = 9), (0, o.postHistoryList)(i);
              case 9:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  toHistory: function (a) {
    var n = this;
    return e(
      t.default.mark(function e() {
        var i;
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                if (
                  (n.setData({
                    collection: null,
                    exhibition: null,
                    activity: null,
                  }),
                  (i = a.currentTarget.dataset.item),
                  n.setData({ isSearched: !0, val: i.name }),
                  n.search(i.name),
                  (t.t0 = i.name),
                  !t.t0)
                ) {
                  t.next = 8;
                  break;
                }
                return (t.next = 8), (0, o.postHistoryList)(i);
              case 8:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  onClear: function () {
    var a = this;
    return e(
      t.default.mark(function e() {
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.next = 2), (0, o.clearHistoryList)();
              case 2:
                a.setData({ historyList: [] }),
                  wx.showToast({
                    title: "历史搜索清空成功",
                    icon: "none",
                    duration: 2500,
                  });
              case 4:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  onClose: function () {
    var a = this;
    return e(
      t.default.mark(function e() {
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                return (t.t0 = a), (t.next = 3), (0, o.getHistoryList)();
              case 3:
                (t.t1 = t.sent),
                  (t.t2 = { val: "", isSearched: !1, historyList: t.t1 }),
                  t.t0.setData.call(t.t0, t.t2);
              case 6:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  onCancel: function () {
    wx.navigateBack();
  },
  toDetailByKey: function (t) {
    var e = t.currentTarget.dataset.item;
    1 === e.type
      ? (0, i.navigateTo)(r.default.PAGE_TOUR_MAP_INDEX, { id: e.poi_id })
      : 2 === e.type
        ? (0, i.navigateTo)(r.default.PAGE_RECOMMEND_EXHIBITION_CARD, {
            id: e.exhibition_id,
          })
        : (0, i.navigateTo)(r.default.PAGE_APPRECIATION_PAGES_CULTURAL_INDEX, {
            id: e.exhibit_id,
          });
  },
  onTab: function (a) {
    var n = this;
    return e(
      t.default.mark(function e() {
        var i;
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                (i = a.currentTarget.dataset.index),
                  n.data.current !== i && n.setData({ current: i });
              case 2:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  toActivityTab: function () {
    this.onTab({ currentTarget: { dataset: { index: 1 } } });
  },
  toExhibitionTab: function () {
    this.onTab({ currentTarget: { dataset: { index: 2 } } });
  },
  toCollectionTab: function () {
    this.onTab({ currentTarget: { dataset: { index: 3 } } });
  },
  toDetailByAll: function (t) {
    this.toDetail(t);
  },
  toDetailByActivity: function (t) {
    this.toDetail(t);
  },
  toDetailByExhibition: function (t) {
    this.toDetail(t);
  },
  toDetailByCollection: function (t) {
    this.toDetail(t);
  },
  scrolltolower: function () {
    var a = this;
    return e(
      t.default.mark(function e() {
        var i,
          r,
          o,
          s,
          c,
          u,
          d,
          f,
          v,
          h,
          p,
          x,
          b,
          y,
          g,
          w,
          D,
          m,
          k,
          _,
          T,
          L,
          O,
          E,
          A,
          P,
          S;
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                if (
                  ((i = a.data),
                  (r = i.current),
                  (o = i.isOver1),
                  (s = i.isOver2),
                  (c = i.isOver3),
                  (u = i.val),
                  (d = i.pn1),
                  (f = i.pn2),
                  (v = i.pn3),
                  (h = i.activity),
                  (p = i.exhibition),
                  (x = i.collection),
                  1 !== r)
                ) {
                  t.next = 13;
                  break;
                }
                if (!o) {
                  t.next = 4;
                  break;
                }
                return t.abrupt("return");
              case 4:
                return (
                  a.setData({ isLoad1: !0 }),
                  (t.next = 7),
                  l.positionSearch({
                    pn: d + 1,
                    keyword: u,
                    lat: a.data.lat,
                    lon: a.data.lon,
                    rn: 5,
                  })
                );
              case 7:
                (g = t.sent),
                  (w =
                    null != g &&
                    null !== (b = g.data) &&
                    void 0 !== b &&
                    b.list.length
                      ? g.data.list.map(function (t) {
                          return (
                            (t.distance = (0, n.getShowDistance)(t.distance)), t
                          );
                        })
                      : []),
                  (D =
                    null != g &&
                    null !== (y = g.data) &&
                    void 0 !== y &&
                    y.total
                      ? g.data.total
                      : 0),
                  a.setData({
                    pn1: d + 1,
                    activity: h.concat(w),
                    isOver1: D >= a.data.activity.length,
                    isLoad1: !1,
                  }),
                  (t.next = 35);
                break;
              case 13:
                if (2 !== r) {
                  t.next = 25;
                  break;
                }
                if (!s) {
                  t.next = 16;
                  break;
                }
                return t.abrupt("return");
              case 16:
                return (
                  a.setData({ isLoad2: !0 }),
                  (t.next = 19),
                  l.exhibitSearch({ pn: f + 1, keyword: u, rn: 5 })
                );
              case 19:
                (_ = t.sent),
                  (T =
                    null != _ &&
                    null !== (m = _.data) &&
                    void 0 !== m &&
                    m.list.length
                      ? _.data.list.map(function (t) {
                          return (
                            (t.distance = (0, n.getShowDistance)(t.distance)), t
                          );
                        })
                      : []),
                  (L =
                    null != _ &&
                    null !== (k = _.data) &&
                    void 0 !== k &&
                    k.total
                      ? _.data.total
                      : 0),
                  a.setData({
                    pn2: f + 1,
                    exhibition: p.concat(T),
                    isOver2: L >= a.data.exhibition.length,
                    isLoad2: !1,
                  }),
                  (t.next = 35);
                break;
              case 25:
                if (3 !== r) {
                  t.next = 35;
                  break;
                }
                if (!c) {
                  t.next = 28;
                  break;
                }
                return t.abrupt("return");
              case 28:
                return (
                  a.setData({ isLoad3: !0 }),
                  (t.next = 31),
                  l.collectionSearch({ pn: v + 1, keyword: u, rn: 5 })
                );
              case 31:
                (A = t.sent),
                  (P =
                    null != A &&
                    null !== (O = A.data) &&
                    void 0 !== O &&
                    O.list.length
                      ? A.data.list.map(function (t) {
                          return (
                            (t.distance = (0, n.getShowDistance)(t.distance)), t
                          );
                        })
                      : []),
                  (S =
                    null != A &&
                    null !== (E = A.data) &&
                    void 0 !== E &&
                    E.total
                      ? A.data.total
                      : 0),
                  a.setData({
                    pn3: v + 1,
                    collection: x.concat(P),
                    isOver3: S >= a.data.collection.length,
                    isLoad3: !1,
                  });
              case 35:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
  toDetail: function (a) {
    return e(
      t.default.mark(function e() {
        var n;
        return t.default.wrap(function (t) {
          for (;;)
            switch ((t.prev = t.next)) {
              case 0:
                (n = a.currentTarget.dataset.item),
                  (0, c.report)("search_result_click", { name: n.name }),
                  1 === n.type
                    ? (0, i.navigateTo)(r.default.PAGE_TOUR_MAP_INDEX, {
                        id: n.poi_id,
                      })
                    : 2 === n.type
                      ? (0, i.navigateTo)(
                          r.default.PAGE_RECOMMEND_EXHIBITION_CARD,
                          { id: n.exhibition_id },
                        )
                      : (0, i.navigateTo)(
                          r.default.PAGE_APPRECIATION_PAGES_CULTURAL_INDEX,
                          { id: n.exhibit_id },
                        );
              case 3:
              case "end":
                return t.stop();
            }
        }, e);
      }),
    )();
  },
});
